//
//  FirstViewController.m
//  homework_UITableView
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()<UITableViewDataSource,UITableViewDelegate,SecondViewControllerDelegate>

@property (nonatomic, retain) NSMutableDictionary *dictionary;

@property (nonatomic, retain) NSArray *keysArray;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSArray *array;

@property (nonatomic, retain) UITableViewCell *cell;//

@end

@implementation FirstViewController

-(void)dealloc
{
    [_tableView release];
    [_cell release];
    [_array release];
    [_keysArray release];
    [_dictionary release];
    [super dealloc];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self handleData];
    [self createSubViews];

}

-(void)config
{
    self.tableView = [[UITableView alloc]init];
        
    self.cell = [[UITableViewCell alloc]init];
    
    self.array = [NSArray array];
    
    self.keysArray = [[NSArray alloc]init];
    
    self.dictionary = [[NSMutableDictionary alloc]init];
    
    self.navigationItem.title = @"联系人";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor orangeColor];
}

#pragma mark - 处理数据
-(void)handleData
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Contact" ofType:@"plist"];
    
    self.dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    
    self.keysArray = [self.dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
}

#pragma mark - 创建视图 UITableView
-(void)createSubViews
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
    
    self.tableView.backgroundColor = [UIColor orangeColor];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.rowHeight = 80;
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    /**创建重用池*/
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.tableView release];
}

#pragma mark - 侧边栏
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.keysArray;
}

#pragma mark - 返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dictionary.count;
}

#pragma mark - 返回每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [self.keysArray objectAtIndex:section];
    
    NSArray *array = [self.dictionary objectForKey:key];
    
    return array.count;
}

#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSArray *array = [self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    self.cell.textLabel.text = [dic objectForKey:@"name"];
   
    NSString *imageName = [dic objectForKey:@"header"];
    
    UIImage *image = [UIImage imageNamed:imageName];
    
    self.cell.imageView.image = image;
    
    self.cell.imageView.layer.cornerRadius = tableView.rowHeight / 2;
        
    self.cell.imageView.clipsToBounds = YES;
    
    return self.cell;
}

#pragma mark - 返回分区的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *headString = [self.keysArray objectAtIndex:section];
    
    return headString;
}

#pragma mark - 进入第二页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    /**取cell的值*/
    NSString *key = [self.keysArray objectAtIndex:indexPath.section];
    
    NSArray *array = [self.dictionary objectForKey:key];
    
    NSDictionary *dic = [array objectAtIndex:indexPath.row];

    SecondViewController *secondVC = [[SecondViewController alloc]init];
    
    secondVC.delegate = self;
    
    secondVC.text = [dic objectForKey:@"name"];
    
    secondVC.image = [UIImage imageNamed:[dic objectForKey:@"header"]];

    secondVC.indexPath = indexPath;
        
    secondVC.nameText = [dic objectForKey:@"name"];
    
    secondVC.QQText = [dic objectForKey:@"QQ"];
 
    secondVC.numText = [dic objectForKey:@"number"];
 
    [self.navigationController pushViewController:secondVC animated:YES];
    
    [secondVC release];
}

/**协议方法*/
-(void)sendMessageWithName:(NSString *)name withNum:(NSString *)num withQQ:(NSString *)QQ withImage:(UIImage *)image withIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    [dic setValue:name forKey:@"name"];
    
    [dic setValue:QQ forKey:@"QQ"];
    
    [dic setValue:num forKey:@"number"];
    
    [dic setValue:image forKey:@"image"];
        
    [self.tableView reloadData];
    
}
@end
















